home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-1.iso / Files / Internet / Misc / Uupc 3.1 sources.sit / uupc 3.1 sources Folder / Mac specific / Unix lib / getcwd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-16  |  1.8 KB  |  114 lines  |  [TEXT/KAHL]

  1. #ifdef THINK_C
  2. # include "unixlibproto.h"
  3. #endif THINK_C
  4.  
  5. #ifndef    THINK_C
  6. #include <memory.h>
  7. #include <aztec/shell.h>
  8.  
  9. #ifdef TEST
  10. #include <stdio.h>
  11. #define _DEBUG
  12. #include <max/debug.h>
  13. #endif
  14. #endif    THINK_C
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include "host.h"
  19.  
  20. #ifndef NULL
  21. #define NULL 0L
  22. #endif
  23.  
  24. #define MACGETCWDFORMAT
  25.  
  26.  
  27. #include "getcwd.proto.h"
  28.  
  29. char * getcwd(char *path, int size)
  30. {
  31.  
  32.     static        char    name[255];
  33.     char        *p, *dir;
  34.     OSErr        rc;
  35.     WDPBRec        wd;
  36.  
  37. #ifdef THINK_C
  38.     memset((char *)(&wd), (int)NULL, (size_t)sizeof(wd));
  39. #else THINK_C
  40.     repmem((char *)&wd, "", 1, sizeof(wd));
  41. #endif THINK_C
  42.     
  43.     if (PBHGetVol(&wd, false) != noErr) {
  44.         return NULL;
  45.     }
  46.  
  47.     if (av_getname(name, wd.ioWDDirID)) {
  48.         return NULL;
  49.     }
  50.     if (*(name+strlen(name)-1) == DIRCHAR) *(name+strlen(name)-1) = '\0';
  51.     dir = name;
  52. #ifndef MACGETCWDFORMAT
  53.     /* convert to Unix style name */
  54.     if ((p = index(name, DIRCHAR)) == NULL) p = name;
  55.     else {
  56.         dir = p;
  57.         *p++ = SEPCHAR;        /* remove vol name and inser leading / */
  58.     }
  59.     /* replace all : with / */
  60.     while (*p) {
  61.         if (*p == DIRCHAR) *p = SEPCHAR;
  62.         p++;
  63.     }
  64. #endif
  65.     if (path != NULL) {
  66.         if (strlen(dir) > size) return(NULL);
  67.         strcpy(path, dir);
  68.         return path;
  69.     }
  70.     else {
  71.         return(dir);
  72.     }
  73. }
  74.  
  75. int av_getname(char *name, long n)
  76. {
  77.     CInfoPBRec    pb;
  78.     OSErr        rc;
  79.     char        space[32];
  80.  
  81. #ifdef THINK_C
  82.         memset((char *)(&pb), (int)NULL, (size_t)sizeof(pb));
  83. #else THINK_C
  84.         repmem((char *)&pb, "", 1, sizeof(pb));
  85. #endif THINK_C
  86.     
  87.     pb.dirInfo.ioNamePtr    = (StringPtr)space;
  88.     pb.dirInfo.ioFDirIndex  = -1;
  89.     pb.dirInfo.ioDrDirID    = n;
  90.         
  91.     if (PBGetCatInfo(&pb, FALSE) != noErr) {
  92.         return -1;
  93.     }
  94.     PtoCstr(space);
  95.     if (n <= 2) {
  96.         strcpy(name, space);
  97.     } else {
  98.         av_getname(name, pb.dirInfo.ioDrParID);
  99.         strcat(name, ":");
  100.         strcat(name, space);
  101.     }
  102.     return 0;
  103. }
  104.  
  105. #ifdef TEST
  106. main(void)
  107. {
  108.  
  109.     fprintf( stderr, "%s\n", getcwd( NULL, 0 ));
  110. }
  111.  
  112. #endif
  113.  
  114.